package com.mic.yzzn.plc.model;

import com.mic.yzzn.plc.common.ins.datastructure.PLCContactorNumber;
import com.mic.yzzn.plc.common.ins.datastructure.PLCDataRegisterNumber;
import com.mic.yzzn.plc.common.toolUtils.PLCContactorCode;
import com.mic.yzzn.plc.common.toolUtils.PLCDataCode;
import com.sun.star.uno.Exception;

public class ContactPoint {

	/** 点位的类型 */
	private String type;
	/** 点位的地址 */
	private String point;
	
	public ContactPoint(){
		
	}
	
	/**
	 * typeAndPoint: e.g. R2000
	 * @param typeAndPoint
	 */
	public ContactPoint(String typeAndPoint){
		this(typeAndPoint.substring(0, 1), typeAndPoint.substring(1, typeAndPoint.length()));
	}
	
	/**
	 * 
	 * @param type R or D
	 * @param point e.g. 2000
	 */
	public ContactPoint(String type, String point) {
		this.type = type;
		if (type.equals(PLCDataCode.PLC_DATA_REGISTER_CODE)) {
			try {
				PLCDataRegisterNumber dNumber = new PLCDataRegisterNumber(point, point);
			} catch (IllegalArgumentException e) {
				this.point = "ILLEGAL";
			}
		} else if (type.equals(PLCContactorCode.PLC_INNER_RELAY_CODE)) {
			try {
				PLCContactorNumber rNumber = new PLCContactorNumber(
						point.substring(0, point.length() - 1),
						point.charAt(point.length() - 1)
				);
			} catch (IllegalArgumentException e) {
				this.point = "ILLEGAL";
			}
		}
		this.point = point;
	}

	@Override
	public String toString() {
		return "ContactPoint [type=" + type + ", point=" + point + "]";
	}

	public String key() {
		return type + point;
	}

	// getters and setters
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getPoint() {
		return point;
	}

	public void setPoint(String point) {
		this.point = point;
	}
	
}
